<template>
  <div>
    <headerXX :name="title"></headerXX>
    <van-pull-refresh
      class="pull-refresh"
      v-model="refreshing"
      @refresh="onRefresh"
    >
      <van-empty
        v-if="list.length == 0 && !error"
        image="search"
        description="未找到数据"
      />
      <van-empty v-if="error" image="error" description="网络错误" />
      <van-list
        :immediate-check="false"
        v-model:loading="loading"
        :finished="finished"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <!-- 列表内容 -->
        <div class="ulbox">
          <div class="ul" v-for="(item, index) in list" :key="index">
            <div class="li">
              <div class="li_left">
                <van-image
                  class="img"
                  radius="4"
                  width="90"
                  height="90"
                  :src="item.companyImage"
                  mode=""
                >
                </van-image>
              </div>
              <div
                class="li_right"
                style="display: flex; flex-direction: column"
              >
                <div @click="jumpToDetail(item)">
                  <div class="minWidth200 marginTop20">
                    <div class="TT">
                      {{ item.companyName }}
                    </div>
                  </div>
                  <div class="minWidth200 marginTop10">
                    <div class="JJ">
                      {{ item.companyAbbr }}
                    </div>
                  </div>
                </div>
                <div
                  class="WZ_size_20"
                  style="
                    position: absolute;
                    bottom: 11px;
                    width: 70%;
                    display: flex;
                    align-items: center;
                  "
                >
                  <van-icon name="location" size="20" />
                  <div class="WZ_size_10">{{ item.companyLocation }}</div>
                </div>
                <div class="li_right_btn" @click="YYXX(item)">立即预约</div>
              </div>
            </div>
          </div>
        </div>
        <template #error>
          <div class="error-tip" @click="retryLoad">
            <van-icon name="warning" />
            <span>加载失败，点击重试</span>
          </div>
        </template>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { testUserMessage } from "../../common/testUser.js";
import headerXX from "../../components/title.vue";
import { getCompanyList } from "@/api/realAssets/icsharedstation/getParent/index.js";
export default {
  components: { headerXX },
  data() {
    return {
      title: "就医服务",
      finished: false,
      error: false,
      loading: false,
      refreshing: false,
      list: [],
      XXyz: {
        siteId: "",
        pageNumber: 1,
        pageSize: 10,
      },
    };
  },
  mounted() {
    testUserMessage("就医服务").then((siteData) => {
      this.siteData = siteData[0];
      this.XXyz.siteId = siteData[0].id;
      this.onLoad();
    });
  },
  methods: {
    YYXX(item) {
      let obj = {
        id: item.id,
        companyAbbr: item.id,
        companyMobile: item.companyMobile,
        companyName: item.companyName,
      };
      // let li = encodeURIComponent(JSON.stringify(obj))
      // uni.navigateTo({
      // 	url: '/pages/pagesNew/BJFW/servicePerson?parentData=' + li,
      // })
      let li = encodeURIComponent(JSON.stringify(obj));
      this.$router.push({
        name: "rvicsharedstation-JYFW_servicePerson",
        query: {
          XX: li, // 序列化对象
        },
      });
    },
    //跳转到详情
    jumpToDetail(item) {
      let li = encodeURIComponent(JSON.stringify(item));
      // console.log(li);
      // 首选方案：使用路由state传递
      this.$router.push({
        name: "rvicsharedstation-JYFW_companyMessage",
        query: {
          XX: li, // 序列化对象
        },
      });
    },
    // 加载数据
    onLoad() {
      this.loading = true;
      if (this.error) return; // 如果当前是错误状态，不自动触发加载
      if (this.refreshing) {
        this.list = [];
      }
      getCompanyList(this.XXyz).then((res) => {
        if (res.resultMessage != "SUCCESS") {
          this.list = [];
          this.error = true; // 显示错误提示
          return;
          // loading.value = false;
        } else if (res.data.length == 0) {
          this.list = [];
          this.finished = true;
        } else {
          this.list = [...this.list, ...res.data];
        }
        console.log(this.list);
        this.loading = false;
        // 判断是否已加载完所有数据
        if (this.list.length == res.pageNum) {
          this.finished = true;
        } else {
          this.XXyz.pageNumber++;
        }
        this.refreshing = false;
      });
      console.log(this.error);
    },
    // onLoad();
    // 点击重试
    retryLoad() {
      this.error = false;
      this.loading = true;
      this.onLoad();
    },
    // 下拉刷新
    onRefresh() {
      //   list.value = [];
      this.XXyz.pageNumber = 1;
      this.XXyz.pageSize = 10;
      this.finished = false;
      this.error = false;
      this.loading = true;
      this.refreshing = true;
      this.onLoad();
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../static/css/index.less";
@import "../../static/css/GYcSS.less";

.TT {
  color: rgb(2, 117, 113);
  font-weight: 600;
  font-size: 13px;
  width: 100%;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}

.JJ {
  display: -webkit-box;
  font-size: 9px;
  line-height: 10px;
  height: 23px;
  width: 100%;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #82848a;
}
</style>